<template>
    <div class="subjectVideostyle">
        <!-- 标题 -->
        <h2>{{ datas.text }}</h2>
        <!-- 表单 -->
        <el-form label-width="80px" :model="datas" size="small">
            <!-- 选择样式 -->
            <el-form-item label="选择样式" class="lef">
                <p style="color: #000">{{ styleText }}</p>
            </el-form-item>
            <!-- 样式选择 -->
            <div class="vidCommodityType">
                <el-tooltip class="item" effect="dark" content="一列一个" placement="bottom">
                    <span
                        class="iconfont icon-shipin"
                        style="font-size: 21px"
                        :class="datas.commodityType === 0 ? 'active' : ''"
                        @click="datas.commodityType = 0"
                    />
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="一列两个" placement="bottom">
                    <span
                        class="iconfont icon-commodity-xiangxiliebiao"
                        style="font-size: 24px"
                        :class="datas.commodityType === 1 ? 'active' : ''"
                        @click="datas.commodityType = 1"
                    />
                </el-tooltip>
            </div>
            <h5 style="color: #000; font-size: 14px">添加内容</h5>
            <p style="color: #969799; font-size: 12px; margin-top: 10px"> 拖动选中的导航可对其排序 </p>
            <!-- 图片广告 -->
            <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
                <transition-group>
                    <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgBanner">
                    <i class="el-icon-circle-close" @click="deleteVideoList(index)" />
                    <!-- 图片和视频 -->
                    <div class="imgAndVideo">
                        <div class="imag">
                            <p>上传背景图</p>
                            <div class="videoimg">
                                <img src="../../../../assets/images/add.png" @click="showUpload('bg',index)" alt draggable="false" v-if="!item.bgUrl" />
                                <img :src="item.bgUrl" @click="showUpload('bg',index)" alt draggable="false" v-else />
                            </div>
                        </div>
                        <div class="imag">
                            <p>上传缩略图</p>
                            <div class="videoimg">
                                <img src="../../../../assets/images/add.png" @click="showUpload('slt',index)" alt draggable="false" v-if="!item.vimgUrl" />
                                <img :src="item.vimgUrl" @click="showUpload('slt',index)" alt draggable="false" v-else />
                            </div>
                        </div>
                        <div class="upvideo">
                            <el-button v-if="!item.videoUrl" size="small" type="primary" @click="uploadVideo(index)">
                                上传视频<i class="el-icon-upload el-icon--right"></i>
                            </el-button>
                            <div v-else>
                                <video width="90" height="90" v-if="item.videoUrl" controls="controls">
                                    <source :src="item.videoUrl" type="video/mp4" />
                                </video>
                                <p @click="deleteupvideo(index)">删除</p>
                            </div>
                        </div>
                    </div>
                    <!-- 链接 -->
                    <div class="imgText">
                        <!-- 选择类型 -->
                        <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink(index)" :linktypeurl="item.linkurl" :theIndex="index" />
                    </div>
                    </section>
                </transition-group>
            </vuedraggable>
            <!-- 添加内容 -->
            <el-button class="uploadImg" style="margin-top:15px; width:100%;" type="success" plain @click="addVideoList">
                <i class="el-icon-plus" />点击添加内容
            </el-button>
        </el-form>
        <!-- 上传图片 -->
        <uploadimg ref="upload" @uploadInformation="uploadInformation" />
        <!-- 上传视频 -->
        <uploadVideo ref="uploadvideo" @uploadVideoSrc="uploadVideoSrc" />
    </div>
</template>
<script>
import vuedraggable from "vuedraggable"; // 拖拽组件
import uploadimg from "../../../uploadImg"; // 图片上传
import SelectLink from "../../../selectLink";
import uploadVideo from "../../../uploadVideo"; // 视频上传
export default {
    name: "SubjectVideostyle",
    components: {
        vuedraggable,
        uploadimg,
        SelectLink,
        uploadVideo
    },
    computed: {
        styleText() {
            let data;
            if (this.datas.commodityType === 0) data = "一列一个";
            if (this.datas.commodityType === 1) data = "一列两个";
            return data;
        },
    },
    props: {
        datas: Object,
    },
    data () {
        return {
            dragOptions: {
                animation: 200,
            },
            curVideoIndex: '',
            changeIndex: '',
            changeType: ''
        }
    },
    methods: {
        addVideoList () {
            this.datas.imageList.push({
                videoUrl: '',
                vimgUrl: '',
                bgUrl: '',
                linkurl: '',
            });
        },
        showUpload(type,index) {
            this.changeType = type;
            this.changeIndex = index;
            this.$refs.upload.showUpload();
        },
        uploadVideo(index) {
            this.curVideoIndex = index;
            this.$refs.uploadvideo.showUpload();
        },
        uploadVideoSrc(url) {
            this.datas.imageList[this.curVideoIndex].videoUrl = url;
        },
        // 删除视频
        deleteupvideo(index) {
            this.datas.imageList[index].videoUrl = '';
        },
        deleteVideoList(index) {
            this.datas.imageList.splice(index, 1);
        },
        // 选择链接
        selectThisLink(resu) {
            this.datas.imageList.forEach((it, ind) => {
                if (ind == resu.index) {
                    it.linkurl = resu.url;
                    it.linktype = resu.linktype;
                    it.id = resu.id;
                }
            });
        },
        clearThisLink (index) {
            this.datas.imageList[index].linkurl = '';
        },
        uploadVideoSrc(url) {
            this.datas.imageList[this.curVideoIndex].videoUrl = url;
        },
        // 提交
        uploadInformation(res) {
            switch (this.changeType) {
                case 'bg':
                    this.datas.imageList[this.changeIndex].bgUrl = res;
                break;
                case 'slt':
                    this.datas.imageList[this.changeIndex].vimgUrl = res;
                break;
            }
        },
    },
}
</script>
<style lang="scss" scoped>
.subjectVideostyle{
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    padding: 0 10px;
    box-sizing: border-box;
    .bor {
        height: 1px;
        margin: 20px 0;
        background-color: #ebedf0;
    }
    /* 标题 */
    h2 {
        padding: 24px 16px 24px 0;
        margin-bottom: 15px;
        border-bottom: 1px solid #f2f4f6;
        font-size: 18px;
        font-weight: 600;
        color: #323233;
    }
    .lef {
        .el-form-item__label {
            text-align: left;
        }
        p {
            line-height: 4px;
        }
    }
    .vidCommodityType {
        display: flex;
        justify-content: space-around;
        align-items: center;
        margin-bottom: 30px;
        span {
            display: inline-block;
            width: 58px;
            height: 32px;
            text-align: center;
            line-height: 32px;
            background: #ebedf0;
            color: #979797;
            border: 1px solid #fff;
            cursor: pointer;
            transition: all 0.5s;
            &:hover {
                border: 1px solid #155bd4;
                color: #155bd4;
            }
            &.active {
                border: 1px solid #155bd4;
                background-color: #e0edff;
                color: #155bd4;
            }
        }
    }
    .imgBanner {
        padding: 6px 12px;
        margin: 16px 7px;
        border-radius: 2px;
        background-color: #fff;
        box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
        // display: flex;
        position: relative;
        /* 删除图标 */
        .el-icon-circle-close {
            position: absolute;
            right: -10px;
            top: -10px;
            cursor: pointer;
            font-size: 19px;
        }
        .imgAndVideo{
            .imag{
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-bottom: 10px;
            }
            .videoimg {
                width: 90px;
                height: 90px;
                border-radius: 5px;
                overflow: hidden;
                position: relative;
                cursor: pointer;
                img {
                    width: 100%;
                    height: 100%;
                    display: inline-block;
                }
                span {
                    background: rgba(0, 0, 0, 0.5);
                    font-size: 12px;
                    position: absolute;
                    left: 0px;
                    bottom: 0px;
                    display: inline-block;
                    width: 100%;
                    text-align: center;
                    color: #fff;
                    height: 20px;
                    line-height: 20px;
                }
            }
        }
        .upvideo{
            padding: 10px 0;
            position: relative;
            p {
                position: absolute;
                left: 0;
                bottom: 0px;
                width: 90px;
                height: 20px;
                font-size: 12px;
                color: #fff;
                background: rgba(0, 0, 0, 0.5);
                text-align: center;
                line-height: 20px;
            }
            :hover{
                cursor: pointer;
            }
        }
        /* 图片字 */
        .imgText {
            width: 100%;
            display: flex;
            flex-direction: column;
            box-sizing: border-box;
            justify-content: space-between;
        }
    }
}
</style>